package com.hjjt.baselib.widget

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams
import android.widget.PopupWindow

/**
 *
 * @ProjectName:    IntelligentTerminal
 * @Package:        com.hjjt.baselib.widget
 * @ClassName:      MenuListPop
 * @Description:
 * @Author:         孙浩
 * @CreateDate:     2022/10/8 16:11
 */
open class MenuListPop(
    var context: Context,
    var viewId: Int,
    var onViewCreated: (v: View) -> Unit
) :
    PopupWindow() {

    init {
        initWindow()
    }

    fun initWindow() {
        var childView = LayoutInflater.from(context).inflate(viewId, null, false)
        onViewCreated.invoke(childView)
        contentView = childView
        setSize()
        isFocusable = true
        isOutsideTouchable = true
        isTouchable = true
        setBackgroundDrawable(ColorDrawable(0X00000000))
        contentView.setOnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                dismiss()
                true
            }
            false
        }
    }

    fun setSize() {
        setSize(null, null)
    }

    fun setSize(width: Int?, height: Int?) {
        if (width == null) {
            setWidth(LayoutParams.MATCH_PARENT)
        } else {
            setWidth(width)
        }
        if (height == null) {
            setHeight(LayoutParams.WRAP_CONTENT)
        } else {
            setHeight(height)
        }
    }

}